package com.highcom.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class RunTimeMonitorAdvice {

    @Pointcut("execution(* com.highcom.service.*Service.find*(..))")
    public void pt(){};

    @Around("pt()")
    public Object runtimeAround(ProceedingJoinPoint pjp) throws Throwable {
        //获取执行签名信息
        Signature signature = pjp.getSignature();
        //获取执行类型（接口名）
        String className = signature.getDeclaringTypeName();
        //获取方法名
        String methodName = signature.getName();
        long sum = 0L; //执行时长累计

        for(int i=0;i<10000;i++) {
            long startTime = System.currentTimeMillis();
            //调用原始方法
            pjp.proceed(pjp.getArgs());

            long endTime = System.currentTimeMillis();
            sum += endTime - startTime;
        }
        System.out.println(className +":" +methodName + "万次run" + sum +"ms");
        return null;
    }

}
